<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="月份日期格式（YYYY-MM）" prop="month">
          <el-input v-model="dataForm.month" placeholder="月份日期格式（YYYY-MM）"></el-input>
      </el-form-item>
          <el-form-item label="当天的日期格式（YYYY-MM-DD）" prop="day">
          <el-input v-model="dataForm.day" placeholder="当天的日期格式（YYYY-MM-DD）"></el-input>
      </el-form-item>
          <el-form-item label="实际转换数量" prop="actEventTypeTotal">
          <el-input v-model="dataForm.actEventTypeTotal" placeholder="实际转换数量"></el-input>
      </el-form-item>
          <el-form-item label="产品公司全称" prop="productCompany">
          <el-input v-model="dataForm.productCompany" placeholder="产品公司全称"></el-input>
      </el-form-item>
          <el-form-item label="媒体公司全称" prop="mediaCompany">
          <el-input v-model="dataForm.mediaCompany" placeholder="媒体公司全称"></el-input>
      </el-form-item>
          <el-form-item label="链接代码" prop="code">
          <el-input v-model="dataForm.code" placeholder="链接代码"></el-input>
      </el-form-item>
          <el-form-item label="产品单价" prop="productPrice">
          <el-input v-model="dataForm.productPrice" placeholder="产品单价"></el-input>
      </el-form-item>
          <el-form-item label="产品结算金额" prop="productAmout">
          <el-input v-model="dataForm.productAmout" placeholder="产品结算金额"></el-input>
      </el-form-item>
          <el-form-item label="媒体单价" prop="mediaPrice">
          <el-input v-model="dataForm.mediaPrice" placeholder="媒体单价"></el-input>
      </el-form-item>
          <el-form-item label="媒体结算金额" prop="mediaAmout">
          <el-input v-model="dataForm.mediaAmout" placeholder="媒体结算金额"></el-input>
      </el-form-item>
          <el-form-item label="利润" prop="profit">
          <el-input v-model="dataForm.profit" placeholder="利润"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
          <el-form-item label="扩展字段" prop="extend">
          <el-input v-model="dataForm.extend" placeholder="扩展字段"></el-input>
      </el-form-item>
          <el-form-item label="产品名称" prop="productName">
          <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
      </el-form-item>
          <el-form-item label="商务" prop="business">
          <el-input v-model="dataForm.business" placeholder="商务"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        month: '',
        day: '',
        actEventTypeTotal: '',
        productCompany: '',
        mediaCompany: '',
        code: '',
        productPrice: '',
        productAmout: '',
        mediaPrice: '',
        mediaAmout: '',
        profit: '',
        createTime: '',
        updateTime: '',
        remark: '',
        extend: '',
        productName: '',
        business: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        month: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        day: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        actEventTypeTotal: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productCompany: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mediaCompany: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productPrice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productAmout: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mediaPrice: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mediaAmout: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        profit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        remark: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        extend: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        productName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        business: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/loanreportday/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/loanreportday/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
